<?php

namespace App\Http\Controllers\Tag;

use App\Helpers\UploadFile;
use App\Models\Live\Anchor;
use App\Models\Tag\Tag;
use App\Models\Tag\TagAttribute;
use App\Models\Tag\TagFinder;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Intervention\Image\Facades\Image;

class TagController extends Controller
{
    //tag

    public function index(Request $request)
    {
        $query = Tag::orderBy('created_at', 'desc');
        $view = view('tag.index')->with('is_anchor', false);

        if ($request->has('s')) {
            $s = trim($request->input('s'));
            $query->where('name', 'like', "%{$s}%");
        }

        if ($request->has('is_rec') && $request->input("is_rec") === 'on') {
            //显示已推荐主播
            $query->where('type', 'anchor')->where('is_rec', 1);
            $view->with('is_anchor', true);
        }

        $tags = $query->paginate(20)->appends($request->only(['s', 'is_rec']));

        return $view
            ->with('tags', $tags)
        ;
    }

    public function create()
    {
        return view('tag.create');
    }

    public function edit(Request $request,Tag $tag)
    {

        $data = $tag->toArray();
        $data['aliases'] = $tag->getAliases();
        $data['similars'] = $tag->getSimilars()->sortBy('r_weight')->values();

        $data['anchor'] = $data['type'] == 'anchor' ? true :false;
        $data['rec_anchor'] = $data['is_rec'] == 1 ? true :false;
        $data['platform'] = $data['type'] == 'platform' ? true :false;
        $data['attributes'] = $tag->attributes()->orderBy('sort','asc')->get()->map(function ($attr) {
            $attr->icon = $attr->icon ? route('image', [$attr->icon, '30x30']) : '';
            return $attr;
        });

        return view('tag.edit')
            ->with('tag', $tag)
            ->with('data', json_encode($data))
        ;
    }

    public function store(Request $request)
    {
        $new = $request->only(['name', 'current_url', 'template']);
        $new['type'] = 'normal';

        if ($request->hasFile('logo')) {
            $new['logo'] = UploadFile::save($request->file('logo'));
        }

        if ($request->hasFile('background_image')) {
            $new['background_image'] = UploadFile::save($request->file('background_image'));
        }

        foreach ($new as $key => $_) {
            $new[$key] = $new[$key] ? $new[$key] : null;
        }

        if (Tag::create($new)) {
            return response()->json(['新增成功！', '继续新增']);
        }
    }

    public function update(Request $request, $tag)
    {
        $new = ['type' => 'normal'];
        $input = $request->all();

//        if (isset($input['anchor']) && $input['anchor'] == 'on') {
//            $new['type'] = 'anchor';
//            $new['anchor_name'] = $input['anchor_name'];
//            $new['link'] = $input['anchor_link'];
//            $new['notice'] = $input['anchor_notice'];
//        } else
        if (isset($input['platform']) && $input['platform'] == 'on') {
            $new['type'] = 'platform';
//            $new['platform_name'] = $input['platform_name'];
            $new['link'] = $input['platform_link'];
            $new['notice'] = $input['platform_notice'];
        }

        $same = ['background_color', 'template', 'name', 'keywords', 'description', 'content', 'current_url'];

        foreach ($same as $key) {
            $new[$key] = $input[$key] ? $input[$key] : null;
        }

        $new['content'] = str_replace("<p></p>",'',$input['content']);
        $new['content'] = str_replace("<p><br></p>",'',$new['content']);
        if ($request->hasFile('logo')) {
            $new['logo'] = UploadFile::save($request->file('logo'));
        }

        if ($request->hasFile('background_image')) {
            $new['background_image'] = UploadFile::save($request->file('background_image'));
        }

        if (Tag::find($input['id'])) {
            Tag::where('id', $input['id'])->update($new);
            $tag = Tag::find($input['id']);
        } else {
            $tag = Tag::create($new);
        }

        if (isset($input['aliases']) && is_array($input['aliases'])) {
            $tag->updateAliases($input['aliases'], true);
        }

        if (isset($input['similars']) && is_array($input['similars'])) {
            $tag->setWeight($tag->updateSimilars($input['similars'], true));
        }

        if (isset($input['anchor']) && $input['anchor'] == 'on') {
            //$tag->bindAnchor(Anchor::where('url', $input['anchor_link'])->first());
            $tag->type = 'anchor';
            if (isset($input['rec_anchor']) && $input['rec_anchor'] == 'on') {
                if (empty($tag->pinyin)) $tag->autoComputePinyin();
                $tag->is_rec = 1;
            }

            $tag->save();
        }

        return response()->json(['msg' => '修改成功！']);
    }

    public function status(Tag $tag, $code)
    {
        Tag::where('id', $tag->id)->update(['status' => $code]);
        return response()->json(['msg' => '操作成功！']);
    }

    public function attrUpdate(Request $request, Tag $tag)
    {
        $attr = $request->only(['name', 'link', 'sort']);
        $attr['tag_id'] = $tag->id;
        if ($request->hasFile('file')) {
            $attr['icon'] = UploadFile::save($request->file('file'), UploadFile::ROOT_DIR.'/'.date('Ymd', time()));
        }

        if ($request->id == 'null') {
            $attr['id'] = TagAttribute::create($attr)->id;
            $attr['msg'] = '添加成功';
        } else {
            TagAttribute::where('id', $request->id)->update($attr);
            $attr['id'] = $request->id;
            $attr['msg'] = '修改成功';
        }

        return response()->json($attr);

    }

    public  function attrDelete(TagAttribute $attr)
    {
        return $attr->delete()
            ? response()->json(['msg' => '删除成功'])
            : response(json_encode(['msg' => '删除失败']), 400)
                ->header('Content-Type', 'application/json');
    }

    public function extractTags(Request $request)
    {
        if (!$request->has('text')) abort(400);

        $text = $request->text;

        return response()
            ->json(TagFinder::extractTags($text, 7));
    }
}
